<?php
$popular_food_name = "34534545 Tuna";
$popular_food_description = "A double layer of sear-sizzled 100% pure beef mingled with special sauce on a sesame seed bun and topped with melty American cheese, crisp lettuce, minced onions and tangy pickles.";

require ('mysqli_connect.php'); // Connect to the db.

function debug_to_console ($data)
{
    if (is_array($data))
        $output = "<script>console.log( 'Debug Objects: " . implode(',', $data) .
                 "' );</script>";
    else
        $output = "<script>console.log( 'Debug Objects: " . $data .
                 "' );</script>";
    
    echo $output;
}
//test by arvind
/*function list_calorie_results()
{
	$searchQuery=$_REQUEST['query'];
	    debug_to_console("Search Query-->". $searchQuery);
}*/
	
// for listing the items based on calories
function list_calories_results ()
{
    // Make the connection:
    $dbc = @mysqli_connect (DB_HOST, DB_USER, DB_PASSWORD, DB_NAME) OR die ('Could not connect to MySQL: ' . mysqli_connect_error() );
    
    // Set the encoding...
    mysqli_set_charset($dbc, 'utf8');
    // Make the query:
    $searchQuery=$_REQUEST['query'];
	$searchQueryUp=$searchQuery+499;
    debug_to_console("Search Query-->". $searchQuery);
	
    $q = "select total_energy, food_id,food_Name, brand_Name,Food_Category,image_url from nutrition_master where total_energy >= " . $searchQuery . " AND total_energy <=" .$searchQueryUp . " limit 1,15 ";
	        debug_to_console("$searchQueryUp");
    $r = @mysqli_query ($dbc, $q); // Run the query.
    
    if ($r) { // If it ran OK, display the records.
    
        // Fetch and print all the records:
        while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC)) {
            
            $foodName = $row['food_Name'];
            $brandName = $row['brand_Name'];
            $food_id = $row['food_id'];
			$calories = $row['total_energy'];
			$categoryName= $row['Food_Category'];
			$image_url = $row['image_url'];
            
           /* echo '<div class="prod_box">
                	<div class="top_prod_box"></div>
                    <div class=".center_prod_box_search">
                         <div class="product_title"><a href="details.php?food_id='. $food_id;
                         echo '">';*/
			echo '<div class="prod_box">
                	<div class="top_prod_box"></div>
                    <div class="center_prod_box">
                         <div class="product_title"><a href="details.php?food_id='. $food_id;
                         echo '">';			 
            echo $foodName;
			//echo $categoryName;
            echo '</a></div>
                         <div class="product_img"><a href="details.php?food_id='. $food_id.'"><img src="';
			echo $image_url;
                 echo'"   width="94" height="92" alt="" title="" border="0" /></a></div>
                 <div class="prod_price"><span class="price">';
                         /*echo '"><img src="../images/mcchicken.png"  width="94" height="92" alt="" title="" border="0" /></a></div>
                         <div class="prod_price"><span class="price">';*/
                        echo $brandName;
						echo "\n Calorie:". $calories . " ";
                        echo'</span></div>
                    </div>
                    <div class="bottom_prod_box"></div>
                    <div class="prod_details_tab">

                    <a href="details.php?food_id='. $food_id;
                         echo '" class="prod_details">details</a>
                    </div>
                </div>';
        }
    
    
        mysqli_free_result ($r); // Free up the resources.
    
    } else { // If it did not run OK.
    
        // Debugging message:
        echo '<p>' . mysqli_error($dbc) . '<br /><br />Query: ' . $q . '</p>';
    
    } // End of if ($r) IF.
    
    mysqli_close($dbc); // Close the database connection.

} // End of the function list_calories_result

?>

<?php
include('header.php');
?>
    
    	<div class="left_content">
   
  
		</div>

		</div>

		<div class="center_content_advanced_search">
            	<?php list_calories_results(); ?>
			</div>
		<!-- end of center content -->

		<div class="right_content">
 
		</div>
		<!-- end of right content -->

<?php
include('footer.php');
?>